﻿using System.Collections.Generic;
using Downloader.Domain;
using Downloader.Framework.Queries;
using Downloader.Infrastructure;
using Downloader.Web.Controllers.ViewModels;
using SharpArch.NHibernate;

namespace Downloader.Web.Controllers.Queries
{
    public class CommentQuery : NHibernateQuery, ICommentQuery
    {
        public RecordList<CommentViewModel> GetList(int page, int size)
        {
            var query = Session.QueryOver<Comment>().OrderBy(c => c.CreateTime).Desc;
               var count = query.ToRowCountQuery();
            var totalCount = count.FutureValue<int>();

            var firstResult = (page - 1) * size;
            var recordList = new RecordList<CommentViewModel>();
            recordList.Data = new List<CommentViewModel>();
            var model = query.Skip(firstResult).Take(size).Future();
            CommentViewModel temp = null;
            foreach (var m in model)
            {
                temp = new CommentViewModel(m);
               // recordList.Data.Add(temp);
            }
            recordList.PageIndex = page;
            recordList.PageSize = size;
            recordList.RecordCount = totalCount.Value;
            return recordList;
        }
    }
}